package com.bbx.taxi.client.Task;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;

import com.bbx.api.sdk.model.official.passanger.Officail_CancelBuild;
import com.bbx.api.sdk.model.official.passanger.Return.OrderList.OfficialCancelPriceDetail;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.api.sdk.net.official.passanger.Officail_CancelNet;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.client.Bean.Value;
/** 
 * @Path com.bbx.taxi.client.Task.MyCancelOrderTask.java
 * @Author 严旭文
 * @Date 2016-2-18 下午5:33:23
 * @Company 蓝海（福建）信息技术有限公司
 * @Description
 * 订单取消
 */
public class MyCancelOrderTask {
	public static int STATUS_SUCCESS=0;
	public static int STATUS_FAIL=-1;
	private Activity context;
	public CancelOrderTask mCancelOrderTask;
	public static MyCancelOrderTask instance;
	public interface OnCancelOrderListener {
		/**
		 * 取消
		 *status 0为成功,
		 */
		public void onCancelOrder(int status);
	}

	static OnCancelOrderListener mOnCancelOrderListener = null;

	public static void setOnCancelOrderListener(OnCancelOrderListener l) {
		mOnCancelOrderListener = l;
	}
	/**
	 * @param context
	 * @param order_id
	 * @param total===cancel_fee
	 * @param cancel_fee
	 * @return
	 */
	public static MyCancelOrderTask getInstance(Activity context,String order_id,String total,int cancel_fee) {
		instance = new MyCancelOrderTask(context,order_id,total,cancel_fee);
		return instance;
	}
	public MyCancelOrderTask(Activity context,String order_id,String total,int cancel_fee) {
		this.context = context;
		getPrice(order_id,total,cancel_fee);
	}
	public void getPrice(String order_id,String total,int cancel_fee){
		Officail_CancelBuild mOfficail_CancelBuild = new Officail_CancelBuild(context);
		mOfficail_CancelBuild.passenger_id=MyApplication.getInstance().getUid();
		mOfficail_CancelBuild.order_id= order_id;
		mOfficail_CancelBuild.cancel_reason="";
		mOfficail_CancelBuild.reason_type=Value.TYPE_NOREASON;
		mOfficail_CancelBuild.cancel_type=Value.CANCEL_PASSENGER;
		OfficialCancelPriceDetail mOfficialCancelPriceDetail=new OfficialCancelPriceDetail();
		try{
			mOfficialCancelPriceDetail.total=Integer.parseInt(total);
		}catch(Exception e){
			mOfficialCancelPriceDetail.total=0;
		}

		mOfficialCancelPriceDetail.cancel_fee=cancel_fee;
		mOfficail_CancelBuild.price_detail=mOfficialCancelPriceDetail;
		
		mCancelOrderTask = new CancelOrderTask(context,
				mOfficail_CancelBuild);
		mCancelOrderTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}
	/**
	 * 取消订单
	 * 
	 */
	private class CancelOrderTask extends BaseAsyncTask {

		private Officail_CancelBuild mOffciail_CancelPriceBuild;

		public CancelOrderTask(Activity context, Officail_CancelBuild mOffciail_CancelPriceBuild) {
			super(context);
			this.mOffciail_CancelPriceBuild = mOffciail_CancelPriceBuild;
		}

		@Override
		public void onSuccess(Object object) {
			if(object!=null){
				if (mOnCancelOrderListener != null) {
					mOnCancelOrderListener.onCancelOrder(STATUS_SUCCESS);
				}
			
			}else{
				if (mOnCancelOrderListener != null) {
					mOnCancelOrderListener.onCancelOrder(STATUS_FAIL);
				}
			}
	

		}

		@Override
		public void onFailed(int status, String msg) {
			if (mOnCancelOrderListener != null) {
				mOnCancelOrderListener.onCancelOrder(STATUS_FAIL);
			}
			
		}
		protected Object doInBackground(Object... params) {
			// TODO Auto-generated method stub
			String json = new JsonBuild().setModel(mOffciail_CancelPriceBuild).getJson();
			return new Officail_CancelNet(context, json);
		}
	}
}
